<?php
/**
 * 通用重排序处理
 * @copyright xinwei
 * @since 2012-03-26
 */

//加载核心class
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();

function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	//数据类型
	cgi::both($g_cgival["data_type"], "data_type", "album_cover_template");
	if(!in_array($g_cgival["data_type"],array("album_cover_template","alliance_recommend")))
		throw new Exception("数据类型错误！");
	
	cgi::both($g_cgival["id"], "id", 0);
	$g_cgival["id"] = intval($g_cgival["id"]);
	if(!$g_cgival["id"]>0)
	{
		throw new Exception("ID错误！");
	}
	//重排order
	cgi::both($g_cgival["orderby"], "orderby", "");
	$g_cgival["orderby"] = string::un_html($g_cgival["orderby"]);
	$g_cgival["orderby"] = string::un_script_code($g_cgival["orderby"]);
	$g_cgival["orderby"] = str_replace("&nbsp;"," ",$g_cgival["orderby"]);
	
	//重排方式(up/down)
	cgi::both($g_cgival["resort_type"], "resort_type", "");
	//当前操作信息ID在列表中的位置
	cgi::both($g_cgival["curr_order"], "curr_order", '');
	$g_cgival["curr_order"] = intval($g_cgival["curr_order"]);
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	
	$operate_obj = "";//操作对象
	$get_list_func_name = "";//获取列表函数名
	$resort_func_name = "";//重排序更新函数名
	$resort_order = " id ";//获取列表order
	$order_row = "";//排序字段名
	
	/*
	 * 数据类型判断(推荐微博/媒体成员)
	 * 说明：指定不同数据类型的排序更新函数名
	 */
	if($g_cgival["data_type"] == "album_cover_template")
	{
		$operate_obj = new db_food_album_cover_template();
		$get_list_func_name = "admin_get_food_album_cover_template_list_by_site_id";
		$function_type = 1;
		$resort_func_name = "admin_update_food_album_cover_template_by_id";
		$order_row = " `order` ";
	}
	elseif($g_cgival["data_type"] == "alliance_recommend")
	{
		$operate_obj = new db_food_alliance_recommend();
		$get_list_func_name = "admin_get_food_alliance_recommend_list_by_site_id";
		$function_type = 2;
		$resort_func_name = "admin_update_food_alliance_recommend_sort_by_id";
		$order_row = " `order` ";
	}
	else 
	{
		throw new Exception("错误的数据类型。");
	}
	/*数据类型判断结束*/
	
	/*
	 * 排序方式处理
	 * 注：orderby参数 与 resort_type参数 互斥
	 * orderby为点击上方排序方式的参数
	 * resort_type为点击向上、向下微调时的参数
	 */
	
	if($g_cgival["orderby"])
	{
		$resort_order = $g_cgival["orderby"];
	}
	elseif($g_cgival["resort_type"])
	{
		$resort_order = $order_row;
	}
	else
	{
		throw new Exception("请选择排序方式");
	}
	/*排序方式处理结束*/
	
	//取得列表
	if($function_type == 1)
	{
		$data_list = $operate_obj->$get_list_func_name($site_id,'',-1,$order_row,$p=1,$pc=2000);
	}
	elseif($function_type == 2)
	{
		$data_list = $operate_obj->$get_list_func_name($site_id,$p=1,$pc=2000);
	}
	

	
	//重排列表
	foreach($data_list as $i => $val)
	{
		if($g_cgival["resort_type"] == "up")
		{
			if($i == ($g_cgival["curr_order"]-1))
			{
				$operate_obj->$resort_func_name($val['id'],$i+1);
			}
			elseif($i == ($g_cgival["curr_order"]))
			{
				$operate_obj->$resort_func_name($val['id'],$i-1);
			}
			else
			{
				$operate_obj->$resort_func_name($val['id'],$i);
			}
		}
		elseif($g_cgival["resort_type"] == "down")
		{
			if($i == ($g_cgival["curr_order"]+1))
			{
				$operate_obj->$resort_func_name($val['id'],$i-1);
			}
			elseif($i == ($g_cgival["curr_order"]))
			{
				$operate_obj->$resort_func_name($val['id'],$i+1);
			}
			else
			{
				$operate_obj->$resort_func_name($val['id'],$i);
			}
		}
		else
		{
			$operate_obj->$resort_func_name($val['id'],$i);
		}
	}
	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();	// 拿到当前管理员ID
	$admin_log = new admin_log();
	
	if($g_cgival["data_type"] == "album_cover_template")
	{
		$admin_log->admin_insert_log_info($admin_id, $site_id, 13, "排序 专辑模板template_id={$g_cgival["id"]}");
	}
	elseif($g_cgival["data_type"] == "alliance_recommend")
	{
		$admin_log->admin_insert_log_info($admin_id, $site_id, 16, "排序 首页美食联盟id={$g_cgival["id"]}");
	}
	
}


try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/resort_handle.php");
	check_cgi_pro();
	get_data();
	$g_show["error"] = "0";
	$g_show["errmsg"] = "";	
}
catch(Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if(isset($_GET['debug']))
	{
		print_r($g_show);
	}
	echo output::json('json',$g_show);
}

?>